package com.xy.core.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class A {
    /**编号*/
    public String id;
    /**概率（0.1代表10%，最多3位小数，即千分之一级）*/
    public float probability;
    /**数量（该类奖品剩余数量）*/
    public int count;

    public A(String id, float probability, int count) {
        super();
        this.id = id;
        this.probability = probability;
        this.count = count;
    }

    public A(){

    }
    
    public static A lottery(List<A> awards){
        //总的概率区间
        float totalPro = 0f;
        //存储每个奖品新的概率区间
        List<Float> proSection = new ArrayList<Float>();
        proSection.add(0f);
        //遍历每个奖品，设置概率区间，总的概率区间为每个概率区间的总和
        for (A award : awards) {
            //每个概率区间为奖品概率乘以1000（把三位小数转换为整）再乘以剩余奖品数量
            totalPro += award.probability * 10 * award.count;
            proSection.add(totalPro);
        }
        //获取总的概率区间中的随机数
        Random random = new Random();
        float randomPro = (float)random.nextInt((int)totalPro);
        //判断取到的随机数在哪个奖品的概率区间中
        for (int i = 0,size = proSection.size(); i < size; i++) {
            if(randomPro >= proSection.get(i) 
                && randomPro < proSection.get(i + 1)){
                return awards.get(i);
            }
        }
        return null;
    }
    
    public static void main(String[] args) {
        List<A> awards = new ArrayList<A>();
        awards.add(new A("a1",0.1f,100));
        awards.add(new A("a2",0.2f,100));
        awards.add(new A("a3",0.3f,100));
        awards.add(new A("a4",0.05f,100));
        awards.add(new A("a5",0.2f,100));
        int num1= 0;
        int num2= 0;
        int num3= 0;
        int num4= 0;
        int num5= 0;
        int num6 =0;
        int num7= 0;
        int iii=1000000;
        String str = "";
        for (int i = 0; i < iii; i++) {
         //   System.out.println("恭喜您，抽到了：" + lottery(awards).id);
            str =lottery(awards).id;
            if("a1".equals(str)){
            	num1++;
            }else
            if("a2".equals(str)){
            	num2++;
            }else
            if("a3".equals(str)){
            	num3++;
            }else
            if("a4".equals(str)){
            	num4++;
            }else
            if("a5".equals(str)){
            	num5++;
            }else{
            	num7++;
            }
        }
        System.out.println(num1+","+Float.valueOf(num1/iii));
        System.out.println(num2+","+Float.valueOf(num2/iii));
        System.out.println(num3+","+Float.valueOf(num3/iii));
        System.out.println(num4+","+Float.valueOf(num4/iii));
        System.out.println(num5+","+Float.valueOf(num5/iii));
        System.out.println(num6);
        System.out.println(num7);
    }
    
}